package cn.byapps.hw.dao;

import cn.byapps.hw.entity.HomeworkFile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface HomeworkFileDao extends JpaRepository<HomeworkFile, String> {
    /**
     * 根据文件保存路径模糊查询
     *
     * @param filepath 文件保存路径
     * @return
     */
    @Query("select (u) from HomeworkFile u where u.filePath like :filepath")
    List<HomeworkFile> findByFilePath(String filepath);

    /**
     * 根据下载地址和文件的MD5值模糊查询
     *
     * @param downloadPath 下载地址
     * @param md5          MD5值
     * @return
     */
    @Query("select (u) from HomeworkFile u where u.filePath like :downloadPath and u.md5 like :md5")
    List<HomeworkFile> findByFilePathAndMd5(String downloadPath, String md5);

    /**
     * 根据作业ID获取所有作业文件
     *
     * @param homeworkId 作业ID
     * @return
     */
    @Query("select (u) from HomeworkFile u where u.homework.id = :homeworkId")
    List<HomeworkFile> findByHomeworkId(Integer homeworkId);
}
